<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use App\Common\Utils\Common;
use App\Common\Utils\ShareViews;
use App\Http\Service\menus\MenusUserService;

class IfAdmin {

    use \App\Helpers\OOS;
    use \App\Helpers\RBAC;

    public function handle($request, Closure $next) {
        if (!Auth::user()) {
            return redirect('/admin/login');
        }
        /*
        if (!Auth::user()->users_status) {
            //获取当前用户的权限
            $permssionDatas = $this->getUserPermissions(Auth::user()->id);
            $personalMenusDatas = (new MenusUserService())->getPersonalMenus();
            $menusDatas = array_merge($permssionDatas, $personalMenusDatas);
            $url = $request->path(); //获取当前不是资源路由的路由;
            //获取当前路由
            $route = Route::currentRouteName();
            //把路由转成资源路由的格式
            if (!empty($route)) {
                $route = str_replace('.', '/', $route);
                $currentRoute = explode('/', $route);
                if (end($currentRoute) === 'index') {
                    array_push($menusDatas, $route);
                }
            } else {
                $route = $url;
            }
            //判断是否有权限并且跳转页面
            if (!in_array($route, $menusDatas)) {
                return redirect('/admin/401');
            }
        }
        //从登陆中获取缓存记录的值
        $loginToken = Cache::get('loginToken');
        if ($loginToken['username'] == Auth::user()->id) {
            //判断局域网络 单点登录
            $moketoken = Common::getSessionToken($request->getSession());
            if ($loginToken['info'] != md5($moketoken . Auth::user()->id)) {
                //此代码在公共文件里面
                $url = $this->single();
                echo "$url";
                exit;
            }
        }
         * 
         */
        return $next($request);
    }

}
